// $codepro.audit.disable defineLoadFactor, questionableAssignment
package com.scribbles.article.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.scribbles.article.dto.ArticleHeaderDTO;
import com.scribbles.article.service.ArticleService;
import com.scribbles.article.util.EntityConversionUtil;
import com.scribbles.common.controller.BaseController;
import com.scribbles.common.dto.ArticleDTO;
import com.scribbles.common.entity.Article;
import com.scribbles.common.entity.ArticleHeader;

@Controller
@RequestMapping(value = "article")
public class ArticleController extends BaseController {
	private Logger logger = Logger.getLogger(ArticleController.class);

	@Autowired
	private ArticleService articleService;
	
	@Autowired
	EntityConversionUtil entityConversionUtil;

	@RequestMapping(value = "upload", method = RequestMethod.GET)
	public String loadUploadPage() {
		return "article/uploadarticle";
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public Map<String, ? extends Object> saveArticle() {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("success", true);
		modelMap.put("message", "Saved successfully");
		return modelMap;
	}

	@RequestMapping(value = "/saveArticleHeader", method = RequestMethod.POST)
	public Map<String, ? extends Object> saveArticleHeader(
			ArticleHeader articleHeader, Integer articleHeaderId) {
		String message = null;
		if (null == articleHeaderId) {
			articleHeader = articleService.createArticleHeader(articleHeader);
			message = articleHeader.getName() + " saved successfully";
		} else {
			articleHeader.setId(articleHeaderId);
			articleService.updateArticleHeader(articleHeader);
			message = articleHeader.getName() + " updated successfully";
		}

		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("success", true);
		modelMap.put("articleHeaderId", articleHeader.getId());
		modelMap.put("message", message);
		return modelMap;
	}

	@RequestMapping(value = "/publishArticleHeader", method = RequestMethod.POST)
	public Map<String, ? extends Object> publishArticleHeader(
			Integer articleHeaderId) {
		String message = null;
		if (null == articleHeaderId) {
			message = "No article header present. Please save the article first";
		} else {
			ArticleHeader articleHeader = articleService
					.publishArticleHeader(articleHeaderId);
			message = articleHeader.getName() + " published !!!";
		}

		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("success", true);
		modelMap.put("message", message);
		return modelMap;
	}

	@RequestMapping(value = "/deleteArticleHeader", method = RequestMethod.POST)
	public Map<String, ? extends Object> deleteArticleHeader(
			Integer articleHeaderId) {
		String message = null;
		if (null == articleHeaderId) {
			message = "No article header present. Please save the article first";
		} else {
			ArticleHeader articleHeader = articleService
					.deleteArticleHeader(articleHeaderId);
			message = articleHeader.getName() + " deleted !!!";
		}

		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("success", true);
		modelMap.put("message", message);
		return modelMap;
	}

	@RequestMapping(value = "/saveArticleDetails", method = RequestMethod.POST)
	public Map<String, ? extends Object> saveArticleDetails(
			Article articlePage, @RequestParam Integer articleHeaderId) {
		String message = null;
		ArticleHeader articleHeader = articleService
				.getArticleHeaderReference(articleHeaderId);
		articlePage.setArticleHeader(articleHeader);

		if (null == articlePage.getId()) {
			articlePage = articleService.createArticle(articlePage);
			message = articlePage.getName() + " created !!! ";
		} else {
			articlePage = articleService.updateArticle(articlePage);
			message = articlePage.getName() + " updated !!! ";
		}

		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("success", true);
		modelMap.put("articleId", articlePage.getId());
		modelMap.put("message", message);
		return modelMap;
	}

/*	@RequestMapping(value = "load/{articleId}", method = RequestMethod.GET)
	public Map<String, Object> loadArticle(
			@PathVariable(value = "articleId") Integer articleId) {
		logger.info("Loading content for article id : " + articleId);
		Map<String, Object> modelMap = new HashMap<String, Object>();
		Article article = articleService.getArticle(articleId);
		ArticleDTO articleDTO = convertArticleEntityToArticleDTO(article);
		// model.addAttribute("articleId", articleId);
		// Gson gson = new Gson();
		// String articleGson = gson.toJson(articleDTO);
		// logger.info("Article GSON : "+ articleGson);
		logger.info("Loaded article : " + articleDTO);
		modelMap.put("article", articleDTO);
		modelMap.put("success", true);
		return modelMap;
	}*/
	
	@RequestMapping(value = "view/{articleHeaderId}", method = RequestMethod.GET)
	public String viewArticle(
			@PathVariable(value = "articleHeaderId") Integer articleHeaderId, Model model) {
		logger.info("Loading content for articleHeaderId : " + articleHeaderId);
		model.addAttribute("articleHeaderId", articleHeaderId);
		return "article/articleView";
	}		
	
	@RequestMapping(value = "loadArticleHeader/{articleHeaderId}", method = RequestMethod.GET)
	public Map<String, Object> loadArticleHeader(
			@PathVariable(value = "articleHeaderId") Integer articleHeaderId) {
		logger.info("Loading content for articleHeaderId : " + articleHeaderId);
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ArticleHeader articleHeader = articleService.getArticleContentForHeaderId(articleHeaderId);
		ArticleHeaderDTO articleHeaderDTO = entityConversionUtil.convertArticleHeaderEntityToArticleHeaderDTO(articleHeader);	
		// model.addAttribute("articleId", articleId);
		// Gson gson = new Gson();
		// String articleGson = gson.toJson(articleDTO);
		// logger.info("Article GSON : "+ articleGson);
		logger.info("Loaded article : " + articleHeaderDTO);
		modelMap.put("articleHeader", articleHeaderDTO);
		modelMap.put("success", true);
		return modelMap;
	}
	
	@RequestMapping(value = "loadArticle/{articleId}", method = RequestMethod.GET)
	public Map<String, Object> loadArticle(
			@PathVariable(value = "articleId") Integer articleId) {
		logger.info("Loading content for articleId : " + articleId);
		Map<String, Object> modelMap = new HashMap<String, Object>();
		Article article = articleService.getArticle(articleId);
		ArticleDTO articleDTO = entityConversionUtil.convertArticleEntityToArticleDTO(article);	
		logger.info("Loaded article : " + articleDTO);
		modelMap.put("article", articleDTO);
		modelMap.put("success", true);
		return modelMap;
	}	
	
	@RequestMapping(value = "getArticlesList/{articleHeaderId}", method = RequestMethod.GET)
	public Map<String, Object> getArticlesList(
			@PathVariable(value = "articleHeaderId") Integer articleHeaderId) {
		logger.info("Get all articles Name for  articleHeaderId : " + articleHeaderId);
		Map<String, Object> modelMap = new HashMap<String, Object>();
		List<Article> articlesList = articleService.getArticlesListForHeader(articleHeaderId);
		List<ArticleDTO> articlesDTOList = entityConversionUtil.convertArticleEntityListToArticleDTOList(articlesList);
		logger.info("Loaded articlesDTOList : " + articlesDTOList);
		modelMap.put("articlesDTOList", articlesDTOList);
		modelMap.put("success", true);
		return modelMap;
	}	



	/*
	 * @RequestMapping(value = "/saveArticle", method = RequestMethod.POST)
	 * public Map<String, ? extends Object> saveArticleHeader(
	 * 
	 * @RequestParam("articleHeaderRecord") String articleHeaderRecord,
	 * 
	 * @RequestParam("articlePageRecord") String articlePageRecord) {
	 * 
	 * Gson gson = new GsonBuilder().setDateFormat("dd/mm/yyyy").create();
	 * ArticleHeader articleHeader = gson.fromJson(articleHeaderRecord,
	 * ArticleHeader.class); logger.info("hi---------- ----- "); Article article
	 * = gson.fromJson(articlePageRecord, Article.class);
	 * logger.info(articleHeader); logger.info(article);
	 * 
	 * Map<String, Object> modelMap = new HashMap<String, Object>();
	 * modelMap.put("success", true); modelMap.put("message",
	 * "Saved successfully"); return modelMap; }
	 */
}
